package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Adulterlink;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("AdulterlinkDAO")
@Transactional
public class AdulterlinkDAOImpl extends AbstractJpaDao<Adulterlink> implements AdulterlinkDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Adulterlink.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public AdulterlinkDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Adulterlink findAdulterlinkById(Integer id) throws DataAccessException {

		return findAdulterlinkById(id, -1, -1);
	}

	@Transactional
	public Adulterlink findAdulterlinkById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdulterlinkById", startResult, maxRows, id);
			return (Adulterlink) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Adulterlink> findAdulterlinkByName(String name) throws DataAccessException {

		return findAdulterlinkByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adulterlink> findAdulterlinkByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdulterlinkByName", startResult, maxRows, name);
		return new LinkedHashSet<Adulterlink>(query.getResultList());
	}

	@Transactional
	public Set<Adulterlink> findAdulterlinkByNameContaining(String name) throws DataAccessException {

		return findAdulterlinkByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adulterlink> findAdulterlinkByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdulterlinkByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Adulterlink>(query.getResultList());
	}

	@Transactional
	public Set<Adulterlink> findAdulterlinkByDescription(String description) throws DataAccessException {

		return findAdulterlinkByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adulterlink> findAdulterlinkByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdulterlinkByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Adulterlink>(query.getResultList());
	}

	@Transactional
	public Set<Adulterlink> findAdulterlinkByDescriptionContaining(String description) throws DataAccessException {

		return findAdulterlinkByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adulterlink> findAdulterlinkByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdulterlinkByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Adulterlink>(query.getResultList());
	}

	@Transactional
	public Adulterlink findAdulterlinkByPrimaryKey(Integer id) throws DataAccessException {
		return findAdulterlinkByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Adulterlink findAdulterlinkByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdulterlinkByPrimaryKey", startResult, maxRows, id);
			return (Adulterlink) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Adulterlink> findAdulterlinksByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdulterlinksByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Adulterlink> findAllAdulterlinks() throws DataAccessException {

		return findAllAdulterlinks(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adulterlink> findAllAdulterlinks(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllAdulterlinks", startResult, maxRows);
		return new LinkedHashSet<Adulterlink>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adulterlink> findAdulterlinkByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Adulterlink>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Adulterlink> findAdulterlinksByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Adulterlink.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Adulterlink> adulterlinks = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return adulterlinks;
	}

	@Transactional
	public Adulterlink findAdulterlinkByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findAdulterlinkByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Adulterlink findAdulterlinkByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdulterlinkByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Adulterlink) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Adulterlink entity) {
		return true;
	}
}
